/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wisimconsole;

/**
 *
 * @author brother
 */
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class CheckList {

    public static void main(String args[]) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create a list containing CheckListItem's

        JList list = new JList(new CheckListItem[]{
                    new CheckListItem(0,"apple"),
                    new CheckListItem(1,"orange"),
                    new CheckListItem(2,"mango"),
                    new CheckListItem(3,"orange"),
                    new CheckListItem(4,"mango"),
                    new CheckListItem(5,"orange"),
                    new CheckListItem(6,"mango"),
                    new CheckListItem(7,"orange"),
                    new CheckListItem(8,"mango"),
                    new CheckListItem(9,"orange"),
                    new CheckListItem(10,"mango"),
                    new CheckListItem(11,"orange"),
                    new CheckListItem(12,"mango"),
                    new CheckListItem(13,"orange"),
                    new CheckListItem(14,"mango"), 
		    new CheckListItem(15,"orange"),
                    new CheckListItem(16,"mango"),
                    new CheckListItem(17,"orange"),
                    new CheckListItem(18,"mango"),
                    new CheckListItem(19,"paw paw"),
                    new CheckListItem(20,"banana")});

        // Use a CheckListRenderer (see below)
        // to renderer list cells

        list.setCellRenderer(new CheckListRenderer());
        list.setSelectionMode(
                ListSelectionModel.SINGLE_SELECTION);

        // Add a mouse listener to handle changing selection

        list.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent event) {
                JList list = (JList) event.getSource();

                // Get index of item clicked

                int index = list.locationToIndex(event.getPoint());
                CheckListItem item = (CheckListItem) list.getModel().getElementAt(index);

                // Toggle selected state

                item.setSelected(!item.isSelected());

                // Repaint cell

                list.repaint(list.getCellBounds(index, index));
            }
        });

        frame.getContentPane().add(new JScrollPane(list));
        frame.pack();
        frame.setVisible(true);
    }
}

